package com.example.medicalzb.controller;

import com.example.medicalzb.model.DrugModel;
import com.example.medicalzb.param.DrugParam;
import com.example.medicalzb.service.DrugService;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/api/drugs")
@Api(tags = "药品控制器类")
@CrossOrigin
public class DrugController {
    @Autowired
    private DrugService drugService;

    @GetMapping("/{pn}/{size}")
    public Msg getDrugWithPage(@PathVariable("pn") Integer pn,
                               @PathVariable("size") Integer size,
                               @RequestParam String name){
        PageInfo<DrugModel> pageInfo = drugService.getDrugWithPage(pn,size,name);
        if (pageInfo != null){
            return Msg.success().data("drugPageInfo",pageInfo);
        }
        return Msg.fail();
    }

    @PostMapping(value = "")
    @RolesAllowed({"1","2"})
    public Msg saveDrug(@RequestBody DrugParam drugParam){
        return drugService.saveDrug(drugParam);
    }

    @PutMapping(value = "{id}")
    @RolesAllowed({"1"})
    public Msg updateDrug(@PathVariable("id") Long id,@RequestBody DrugParam drugParam){
        return drugService.updateDrug(id,drugParam);
    }

    @DeleteMapping(value = "/{drugId}")
    @RolesAllowed({"1"})
    public Msg deleteDrug(@PathVariable("drugId") Long id){
        return drugService.deleteDrug(id);
    }
}
